package pro.cherish.knowledgeutil.map;

import cn.hutool.core.map.MapUtil;
import pro.cherish.knowledgeutil.str.StringUtilExt;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 创建人：cherish
 * @version 版本号：V1.0
 * <p>
 * ***************************修订记录************************************
 * 2019-11-01 cherish 创建该类功能。
 * **********************************************************************
 * </p>
 * @ClassName 类名：MapUtilsExt
 * @Description 功能说明：
 * <p>
 * TODO
 * </p>
 * **********************************************************************
 * @date 创建日期： 2019-11-01 cherish
 */
public class MapUtilsExt extends MapUtil {

    /**
     * 循环拷贝一个新的对象
     *
     * @param map 源map对象
     * @return
     */
    public static Map<String, Object> copyNewMap(Map<String, Object> map) {
        Map<String, Object> result = new HashMap<String, Object>();
        for (String key : map.keySet()) {
            result.put(key, map.get(key));
        }
        return result;
    }

    /**
     * 转换Map<String,Object>为 Map<String,String>
     *
     * @param map 源map对象
     * @return
     */
    public static Map<String, String> ObjectToStringMap(Map<String, Object> map) {
        Map<String, String> result = new HashMap<String, String>();
        for (String key : map.keySet()) {
            result.put(key, StringUtilExt.toString(map.get(key), ""));
        }
        return result;
    }

    /**
     * 转换Map<String,String>为 Map<String,Object>
     *
     * @param map 源map对象
     * @return
     */
    public static Map<String, Object> StringToObjectMap(Map<String, String> map) {
        Map<String, Object> result = new HashMap<String, Object>();
        for (String key : map.keySet()) {
            result.put(key, StringUtilExt.toString(map.get(key), ""));
        }
        return result;
    }


    /**
     * 获取map指定key的值
     *
     * @param map
     * @param key
     * @return
     */
    public static String getString(Map<String, Object> map, String key) {
        Object value = map.get(key);
        if (value != null) {
            return value.toString();
        } else {
            return null;
        }
    }

    /**
     * 获取Map指定key的Map
     *
     * @param map
     * @param key
     * @return
     */
    public static Map getMap(Map<String, Object> map, String key) {
        if (map == null) {
            return null;
        } else {
            Object o = map.get(key);
            if (o instanceof Map) {
                return (Map) o;
            } else {
                return null;
            }
        }
    }

}
